﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace StockholmsStad.Windows.UI.Infrastructure.Converters
{
    public class DateTimeToDayString : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(string) && targetType != typeof(object))
                throw new InvalidOperationException("The target must be a string");

            if (value != null)
            {
                var dateTime = (DateTime)value;
                return dateTime.ToString("dd");
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(object) && value != null)
            {
                DateTime result;
                DateTime.TryParse(value.ToString(), out result);
                return result;
            }

            return value;
        }

        #endregion
    }
}
